#include <algorithm>
#include <cctype>
#include <cstdio>
#define ri register int
//TODO:
const int N = 2500;
typedef long long ll;
int n, mod, cnt, tot[N], tysum[N], tot3[N];
int a[N], trans[N];
int ans1, ans2, ans3;

ll qpow(ll base, int index) {
	ll res = 1;
	while (index) {
		if (index & 1) res = res * base % mod;
		base = base * base % mod;
		index >>= 1;
	}
	return res;
}

inline bool cmp(const int &a, const int &b) {
	return a % mod == b % mod ? a < b : a % mod < b % mod;
}

inline int binsearch(int x) {
	ri l = 1, r = cnt, mid;
	while (l < r) {
		mid = (l + r) >> 1;
		if (trans[mid] < x)
			l = mid + 1;
		else
			r = mid;
	}
	return trans[l] == x ? l : -1;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
	freopen("T1.in", "r", stdin);
	freopen("T1.out", "w", stdout);
#endif
#endif

	scanf("%d%d", &n, &mod);
	for (ri i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
	}
	std::sort(a + 1, a + n + 1, cmp);
	for (ri i = 1; i <= n; ++i) {
		if (a[i] % mod != a[i - 1] % mod) ++cnt, trans[cnt] = a[i] % mod;
		if (a[i] == a[i - 1] && a[i] != a[i + 1]) ++tysum[cnt];
		if (a[i] != a[i - 1]) ++tot[cnt];
		if (a[i] == a[i - 1] && a[i] == a[i - 2] && a[i] != a[i + 1]) ++tot3[cnt];
	}
	for (ri i = 1; i <= cnt; ++i) {
		for (ri j = 1; j <= cnt; ++j) {
			ri inv = qpow(trans[i] * (long long)trans[j] % mod, mod - 2);
			ri pos = binsearch(inv);
			if (pos == -1) continue;
			if (i != j && j != pos && i != pos)
				ans1 += (tot[i] * tot[j] * tot[pos]);
			else if (i != j && j == pos) {
				ans1 += (tot[i] * (tot[j] - 1) * tot[j]);
				ans2 += (tysum[j] * tot[i]);
			} else if (i != j && i == pos) {
				ans1 += (tot[j] * (tot[i] - 1) * tot[i]);
				ans2 += (tysum[i] * tot[j]);
			} else if (i != pos && j == i) {
				ans1 += tot[pos] * (tot[i] - 1) * tot[i];
				ans2 += tysum[i] * tot[pos];
			} else {
				ans1 += tot[i] * (tot[i] - 1) * (tot[i] - 2);
				ans2 += tysum[i] * (tot[i] - 1) * 3;
				ans3 += tot3[i];
			}
		}
	}
	printf("%d", ans1 / 6 + ans2 / 3 + ans3);
	return 0;
}